/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAL.DABasic;
import Entity.Employee;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author HOANG ANH
 */
public class MainController {

    private DABasic dal;
    private Connection conn;

    public MainController() {
        dal = new DABasic();
    }

    public int Register(Employee emp) throws SQLException, ClassNotFoundException {

        conn = dal.getConnection();
        PreparedStatement ps = conn.prepareStatement("INSERT INTO [employees]"
                + "([name],[country],[isMale],[birthday])"
                + "VALUES (?,?,?,?)");
        ps.setString(1, emp.getName());
        ps.setString(2, emp.getCountry());
        ps.setBoolean(3, emp.isMale());
        SimpleDateFormat formater = new SimpleDateFormat("dd MMMM yyyy");
        ps.setString(4, formater.format(emp.getBirthday()));

        int result = ps.executeUpdate();
        ps.close();
        conn.close();
        return result;
    }

    public ArrayList<Employee> getListEmployee() throws SQLException, ClassNotFoundException, ParseException {
        ArrayList<Employee> list = new ArrayList<Employee>();
        conn = dal.getConnection();
        PreparedStatement ps = conn.prepareStatement("SELECT * FROM [employees]");
        ResultSet rs = ps.executeQuery();
        
        while (rs.next()) {            
            String name = rs.getString("name");
            String country = rs.getString("country");
            boolean male = rs.getBoolean("isMale");
            SimpleDateFormat formater = new SimpleDateFormat("dd MMMM yyyy");
            Date birthday = formater.parse(rs.getString("birthday"));
            
            Employee emp = new Employee(name, country, birthday, male);
            list.add(emp);
        }
        rs.close();
        ps.close();
        conn.close();
        return list;
    }

    public int delEmployee(String name) throws SQLException, ClassNotFoundException{
        conn = dal.getConnection();
        PreparedStatement ps = conn.prepareStatement("DELETE FROM [employees] WHERE [name] = ?");
        ps.setString(1, name);
        int executeUpdate = ps.executeUpdate();
        ps.close();
        conn.close();
        return executeUpdate;
    }

    public int updateEmployee(Employee emp) throws SQLException, ClassNotFoundException{
        conn = dal.getConnection();
        PreparedStatement ps = conn.prepareStatement
                ("UPDATE [employees] "
                + "SET [country] = ?, [isMale] = ?, [birthday] = ?"
                + " WHERE [name] = ?");
        ps.setString(1, emp.getCountry());
        ps.setBoolean(2, emp.isMale());

        SimpleDateFormat formater = new SimpleDateFormat("dd MMMM yyyy");
        ps.setString(3, formater.format(emp.getBirthday()));

        ps.setString(4, emp.getName());
        
        int executeUpdate = ps.executeUpdate();
        ps.close();
        conn.close();
        return executeUpdate;
    }
}
